import { Component, OnInit, ViewChild } from '@angular/core';
import { SFComponent, SFSchema, SFUISchema } from '@delon/form';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalService } from 'ng-zorro-antd/modal';
import { UsermanageService } from 'src/app/routes/usercenter/services/usercenter.service';
import { ImageViewComponent } from 'src/app/shared/components/imagelist';

@Component({
  selector: 'app-audit-admin',
  templateUrl: './audit-admin.component.html',
  styleUrls: ['./audit-admin.component.less']
})
export class AuditAdminComponent implements OnInit {
  @ViewChild('sf', { static: false })
  sf!: SFComponent;
  i: any;
  schema!: SFSchema;
  ui: SFUISchema = {
    '*': {
      spanLabelFixed: 120,
      grid: { span: 20 }
    }
  };
  isReadOnly = false;
  constructor(public msgSrv: NzMessageService, private nzModalService: NzModalService, public service: UsermanageService) {}

  ngOnInit(): void {
    this.loadUserInfo();
  }

  loadUserInfo() {
    this.service.request(this.service.$api_get_enterprise_user_by_id, { id: this.i.id }).subscribe(res => {
      if (res) {
        this.initSF(res);
      }
    });
  }

  show(src: string) {
    if (!src) {
      return;
    }
    const params = {
      imgList: [src],
      index: 0
    };
    this.nzModalService.create({ nzContent: ImageViewComponent, nzComponentParams: { params } });
  }

  initSF(user?: any) {
    // console.log(user);

    this.schema = {
      properties: {
        expand: { type: 'boolean', ui: { hidden: true } },
        oldAdminName: {
          title: '当前管理员',
          type: 'string',
          ui: { widget: 'text' },
          default: user.oldAdminName
        },
        oldAdminMobile: {
          title: '手机号',
          type: 'string',
          ui: { widget: 'text' },
          default: user.oldAdminMobile
        },
        oldAdminIdNo: {
          title: '身份证号',
          type: 'string',
          ui: { widget: 'text' },
          default: user.oldAdminIdNo
        },
        newAdminName: {
          title: '转授对象',
          type: 'string',
          ui: { widget: 'text' },
          default: user.newAdminName
        },
        newAdminMobile: {
          title: '手机号',
          type: 'string',
          ui: { widget: 'text' },
          default: user.newAdminMobile
        },
        newAdminIdNo: {
          title: '身份证号',
          type: 'string',
          ui: { widget: 'text' },
          default: user.newAdminIdNo
        },
        newPhotoFrontWatermark: {
          title: '身份证照',
          type: 'string',
          ui: { widget: 'custom' },
          default: user
        },
        creditPhoto: {
          title: '企业授权函',
          type: 'string',
          ui: { widget: 'custom' },
          default: user.creditPhoto
        },
        approvalOpinion: {
          title: this.isReadOnly ? '驳回原因' : '备注',
          type: 'string',
          maxLength: 100,
          ui: {
            placeholder: '审核不通过需要说明原因',
            widget: this.i?.approvalStatus === 30 && this.isReadOnly ? 'text' : 'textarea',
            autosize: { minRows: 3, maxRows: 6 },
            visibleIf: {
              expand: (value: boolean) => !this.isReadOnly || this.i?.approvalStatus === 30
            }
          },
          default: user.approvalOpinion
        }
      }
    };
  }
}
